package ru.jexp.rcp.supply.beans.ek;

import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;

import ru.jexp.rcp.supply.beans.project.dvs.sostavizdelia.EkTreeViewerNPresenter;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.n.ColumnPair;
import ru.jexp.rcp.supply.n.JExpTreeViewerN;
import ru.jexp.rcp.supply.n.commands.HandlerEditSostavIzdelia;
import ru.jexp.rcp.supply.swtext.JExpMouseAdapter;

public class JExpTreeViewerNEk extends JExpTreeViewerN implements EkTreeViewerNPresenter.IView
{

	public JExpTreeViewerNEk(Composite parent)
	{
		super(parent, createColumns());
		getTree().addMouseListener(new JExpMouseAdapter()
		{
			@Override
			public void mouseUpRight(MouseEvent e)
			{
				TreeItem item = getTree().getItem(new Point(e.x, e.y));
				if (item != null)
				{
					getTree().setSelection(item);
				}
				else
				{
					getTree().deselectAll();
					JExpTreeViewerNEk.this.firePostSelectionChanged(new SelectionChangedEvent(JExpTreeViewerNEk.this, new StructuredSelection()));
				}
			}
		});
		addDoubleClickListener(new IDoubleClickListener()
		{
			@Override
			public void doubleClick(DoubleClickEvent event)
			{
				IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
				try
				{
					if (!getSelection().isEmpty())
					{
						service.executeCommand(HandlerEditSostavIzdelia.ID, null);
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});

//		setAutoExpandLevel(4);
//
//		addDragSupport(DND.DROP_MOVE, new Transfer[] { TextTransfer.getInstance() }, new EkTreeViewerDrag(this));
//		addDropSupport(DND.DROP_MOVE, new Transfer[] { TextTransfer.getInstance() }, new EkTreeViewerDrop(this));
	}

	private static ColumnPair[] createColumns()
	{
		return new ColumnPair[] {	ColumnPair.string(JExpSupplyMessages.Ek_column_name),
									ColumnPair.string(JExpSupplyMessages.Ek_column_dvs),
									ColumnPair.deciaml(JExpSupplyMessages.Ek_column_kolvo),
									ColumnPair.string(JExpSupplyMessages.Ek_column_edizmer),
									ColumnPair.string(JExpSupplyMessages.Ek_column_pf),
									ColumnPair.string(JExpSupplyMessages.Ek_column_supply),
									ColumnPair.string(JExpSupplyMessages.Ek_column_status), };
	}

	@Override
	public TreeViewer getViewer()
	{
		return this;
	}

}
